﻿$(function () {
    $(document).on('click', '.sp-item .checkbox div', function () {
        if ($(this).hasClass('icon-gou')) {
            $(this).removeClass('icon-gou');
        }
        else $(this).addClass('icon-gou');
        CalSumPrice();
    })
    $('.submit-good-wrapper .checkbox div').add('.shopping-title .checkbox div').click(function () {
        if ($(this).hasClass('icon-gou')) {
            $('.checkbox div').removeClass('icon-gou');
        }
        else {
            $('.checkbox div').addClass('icon-gou');
        }
        CalSumPrice();
    });
    $(document).on('click', '.sp-item .del', function () {
        if (window.confirm("确认删除" + $(this).siblings('.good').eq(0).html() + "吗")) {
            $(this).parent('.sp-item').remove();
            $.ajax({
                url: "../Store/DeleteSingleShoppingCar",
                type: 'post',
                data: {
                    id: $(this).attr('id')
                }
            });
        }
        CalSumPrice();
    });

    $(document).on('change', '.sp-item .num input', function () {
        if (parseInt($(this).val()) < 0) {
            $(this).val(0);
            return;
        }
        $(this).parent().siblings('.sum').children('.rmb').html('￥' + parseInt($(this).val()) * parseInt($(this).parent().siblings('.price').children('.rmb').html().replace('￥', '')));
        CalSumPrice();
        $.ajax({
            url: "../Store/UpdateNum",
            type: 'post',
            data: {
                id: $(this).attr('id'),
                num: $(this).val()
            }
        })
    })

    //计算总价
    var rmb = 0.00, ced = 0, count = 0;
    function CalSumPrice() {
        rmb = 0.00, ced = 0, count = 0;
        $('.sp-item .sum .rmb').filter(function (index) {
            return $(this).parent().siblings('.checkbox').children('div').hasClass('icon-gou');
        }).each(function () {
            rmb += parseFloat($(this).html().replace("￥", ""));
            count++;
        });
        $('.submit-good-wrapper .sub-sum .sum span').eq(0).html('￥ ' + rmb + ".00");
        $('.submit-good-wrapper .sub-sum .small span').eq(0).html(count);

    }
    CalSumPrice();

    //批量删除
    var json = {};
    $('body').on('click', '.submit-good-wrapper .del-sel', function () {
        if (window.confirm('您是否要删除勾选的商品？')) {
            $('.sp-item').filter(function () {
                return $(this).find('.vam').hasClass('icon-gou');
            }).each(function () {
                json[$(this).attr('id')] = 0;
                $(this).remove();
            });
            if (!$.isEmptyObject(json)) {
                $.ajax({
                    url: '../Store/DeleteShoppingCars',
                    type: 'post',
                    data: { json: JSON.stringify(json) },
                });
            }
        }
    })

    //结算
    var sjson = {};
    $('.submit-good-wrapper button').click(function () {
        $('.sp-item').filter(function () {
            return $(this).find('.vam').hasClass('icon-gou');
        }).each(function () {
            sjson[$(this).attr('id')] = 0;
        });
        if (!$.isEmptyObject(sjson)) {
            window.location.href = '../Store/CarToPlaceOrder?json=' + JSON.stringify(sjson);
        }
    });
})
